<template>
    <block v-if="scene == 1154">
        <pyq-open-chunk></pyq-open-chunk>
    </block>
    <block v-else>
        <view class="page col-3e bg-fff">
            <up-tabs :list="kaiList" :current="currKai" lineColor="#007235" keyName="title" @click="changeKai"></up-tabs>

            <!-- <building-correlation :buildings="buildings" :imgsList="imgsList" v-if="buildings.length > 0 || imgsList.length > 0"></building-correlation> -->

            <building-averages :list="builAveraList" :productId="productId" :kaiId="kaiId" v-if="builAveraList && builAveraList.length > 0"></building-averages>

            <view class="col-grey text-center top-20 bottom-15 font12">暂无更多数据</view>

			<!-- <bind-seller-bottom-bar :detail="recommendAdviser" v-if="recommendAdviser&&recommendAdviser.avatar_url"></bind-seller-bottom-bar> -->

            <!-- <product-detail-btn :productId="productId" :title="title" v-if="share"></product-detail-btn> -->
        </view>
    </block>
</template>
<script>
    // import BuildingCorrelation from './components/building-correlation.vue';
    import BuildingAverages from './components/building-averages.vue';
	import { mapState, mapActions } from 'pinia';
    import { useUserInfoStore, useConfigStore } from '@/store/index';
    import { concurRequest, isBoolean } from '@/common/common';
	import { panListAPI } from "@/common/api";
    // import { dispatchClient } from '@/common/api-adviser';
    // import { getSellerInfoById } from '@/common/api/adviser';

	export default {
        components: {
            // BuildingCorrelation,
            BuildingAverages
        },
		data() {
			return {
                priceTableData: null,
                kaiId: '',
                currKai: -1,
                kaiList: [],
                kaiIdHistory: [],
                menus: [
                    // {
                    //     title: '日照分析',
                    //     value: 'sunshine',
                    //     url: '/product/mall/v6/pan/sun/',
                    //     icon: 'https://image.srea.org.cn/xcx/icon/icon-strategy-sunshine.png'
                    // },
                    // {
                    //     title: '户型分布',
                    //     value: 'distribution',
                    //     url: '/product/mall/smart/housetype/',
                    //     icon: 'https://image.srea.org.cn/xcx/icon/icon-strategy-house-type-distribute.png'
                    // },
                    // {
                    //     title: '小区不利因素',
                    //     value: 'disadvantage',
                    //     url: '/product/mall/v6/pan/bad/',
                    //     icon: 'https://image.srea.org.cn/xcx/icon/icon-strategy-factors.png'
                    // },
                    // {
                    //     title: '楼栋均价分布',
                    //     value: 'priceDistribution',
                    //     url: '/product/mall/v6/pan/avgprice/',
                    //     icon: 'https://image.srea.org.cn/xcx/icon/icon-strategy-factors.png'
                    // },
                    // {
                    //     title: '栋距图',
                    //     value: 'buildingSpacing',
                    //     url: '/product/mall/v6/pan/distance/',
                    //     icon: 'https://image.srea.org.cn/xcx/icon/icon-strategy-building-spacing.png'
                    // },
                    {
                        title: '栋列均价图',
                        value: 'buildingAverage',
                        url: '/product/mall/smart/houseprice/',
                    }
                ],
                title: '',
				productId: '',
                share: false,
                start_time: 0,
                recommendAdviser: {}
            }
		},
        computed:{
            ...mapState(useUserInfoStore, ['userInfo']),
            ...mapState(useConfigStore, ['puid', 'scene']),
            buildings: function() {
                if(this.kaiList.length === 0 || !this.kaiList[this.currKai]) return [];
                if(this.kaiList[this.currKai].hasOwnProperty('distribution')) {
                    return this.kaiList[this.currKai]['distribution']['building_list'] || []
                }
                return []
            },

            imgsList: function() {
                let imgsArr = [];
                const currKaiObj = this.kaiList[this.currKai];
                if(this.kaiList.length === 0 || !currKaiObj) return [];

                //户型分布
                if(currKaiObj.hasOwnProperty('distribution')) {
                    if(currKaiObj['distribution']['house'].length > 0) {
                        imgsArr.push(currKaiObj['distribution']['house'][0])
                    }
                }

                //不利因素
                if(currKaiObj.hasOwnProperty('disadvantage')) {
                    if(currKaiObj['disadvantage'].length > 0) {
                        imgsArr.push(currKaiObj['disadvantage'][0])
                    }
                }

                //日照
                if(currKaiObj.hasOwnProperty('sunshine')) {
                    const dongZhiImgs = currKaiObj['sunshine'].filter(item => item.jq === 2);
                    if(dongZhiImgs.length > 2){
                        imgsArr.push(dongZhiImgs[dongZhiImgs.length-2])
                    }
                }

                //楼栋均价分布
                if(currKaiObj.hasOwnProperty('priceDistribution')) {
                    if(currKaiObj['priceDistribution'].length > 0) {
                        imgsArr.push(currKaiObj['priceDistribution'][0])
                    }
                }

                //栋距图
                if(currKaiObj.hasOwnProperty('buildingSpacing')) {
                    if(currKaiObj['buildingSpacing'].length > 0) {
                        imgsArr.push(currKaiObj['buildingSpacing'][0])
                    }
                }
                return imgsArr
            },

            //栋列均价分析图列表
            builAveraList: function () {
                const currKaiObj = this.kaiList[this.currKai];
                if(this.kaiList.length === 0 || !currKaiObj) return [];

                if(currKaiObj.hasOwnProperty('buildingAverage')) {
                    return currKaiObj['buildingAverage']['size_list']
                }
                return []
            }
        },
        onShareAppMessage() {
            return {
                title: `【${this.title}】各楼栋一房一价分析`,
                path: `/pages/product/one-house-one-price/one-house-one-price-column-average?productId=${this.productId}&kaiId=${this.kaiId}&title=${this.title}&puid=${this.userInfo.id}&share=true&prepage=各楼栋一房一价分析-${this.productId}-${this.title}`,
            }
        },
        
        //#ifdef MP-WEIXIN
        onShareTimeline() {
            return {
                title: `【${this.title}】各楼栋一房一价分析`,
                query: `productId=${this.productId}&kaiId=${this.kaiId}&title=${this.title}&puid=${this.userInfo.id}&share=true&prepage=一房一价分析表-${this.productId}-${this.title}`
            }
        },
        //#endif

        onAddToFavorites() {
            return {
                title: `【${this.title}】各楼栋一房一价分析`,
                query: `productId=${this.productId}&kaiId=${this.kaiId}&title=${this.title}&puid=${this.userInfo.id}&share=true&prepage=一房一价分析表-${this.productId}-${this.title}`
            }
        },
        
		onLoad(options) {
            const that = this;
            
            //朋友圈进入小程序快照场景
            if(this.scene == 1154) return;
            
            let { productId='', kaiId='', puid='', title='', share=false } = options;
            this.productId = productId&&productId!=='undefined'?productId:'';
            this.title = title&&title!=='undefined'?title:'';
            this.share = isBoolean(share);
            this.start_time = (new Date()).getTime();
            if(puid&&puid!=='undefined') {
                this.setPuid(puid)
            }
            //有开盘id先请求一房一价电子表,否则请求一房一价图片
            if(kaiId && kaiId !== "null") {
                this.kaiId = kaiId;
                uni.$check_and_login(function () {
                    that.init()
                })
            }
		},

        methods: {
			...mapActions(useConfigStore, ['setPuid']),

            async init() {
                //绑定客户
                // await dispatchClient({
                //     share: this.share,
                //     puid: this.puid,
                //     pid: this.productId
                // });
                
                this.getPanList();

                //根据顾问ID获取被绑定的顾问信息
                // if(this.puid && this.puid !== this.userInfo.id) {
                //     let sellInfoRes = await getSellerInfoById({id: this.puid, productId: this.productId});
                //     if(sellInfoRes.status === 0) {
                //         if(sellInfoRes.detail.product_id && sellInfoRes.detail.product_id == this.productId) {
                //             this.recommendAdviser = sellInfoRes.detail;
                //         }
                //     }
                // }
            },
            //获取开盘id
            async getPanList(){
                let res = await panListAPI(this.productId);
                if(res.status !== 0) {
                    uni.hideLoading();
                    uni.$u.toast(res.detail);
                    return
                }
                if(res.detail.length > 0) {
                    this.kaiList = res.detail;
                    let kaiIdCurr = this.kaiList.findIndex(kai => kai.id == this.kaiId);
                    await this.changeKai({index: kaiIdCurr>-1?kaiIdCurr:0});
                }
            },

            //切换开盘
            async changeKai({index=0}){
                let isFirst = this.kaiIdHistory.findIndex(item => item === this.kaiList[index].id);
                if(isFirst < 0) {
                    uni.showLoading({
                    	title: '加载中'
                    });
                    let urls = this.menus.map(item => item.url+this.kaiList[index].id);
                    let resArr = await concurRequest(urls, 3);
                    for(let i=0; i< resArr.length; i++) {
                        if(resArr[i].status === 0) {
                            this.kaiList[index][this.menus[i].value] = resArr[i].detail;
                        }else {
                            this.kaiList[index][this.menus[i].value] = [];
                        }
                    }
                    this.kaiIdHistory.push(this.kaiList[index].id);
                    uni.hideLoading();
                }
                this.currKai = index;
                this.kaiId = this.kaiList[index].id;
                let currKaiObj = this.kaiList[index];
                for(let i=0; i<this.menus.length; i++) {
                    if(this.menus[i].value === 'buildingAverage') {
                        this.title = currKaiObj['buildingAverage'].product_title;
                    }
                }

                currKaiObj = null;
            }
		}
	}
</script>

<style lang="scss" scoped>
	.page{
        line-height: 1.4;
        min-height: 100vh;
        overflow: hidden;
	}
</style>
